package com.nvyuan.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        while (true){
            System.out.println("等待连接");
            //堵塞方法
            Socket client = serverSocket.accept();
            System.out.println("有客户端连接了");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler(client);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }).start();



        }
    }

    private static void handler(Socket client) throws IOException {
        byte[] bytes = new byte[1024];
        System.out.println("准备read");
        //接收客户端的数据，堵塞方法，没哟数据可读时候堵塞
        int read = client.getInputStream().read(bytes);
        System.out.println("read完毕......");
        if(read!=-1){
            System.out.println("接收到客户端的消息"+new String(bytes,0,read));
        }



    }
}
